/*
 * @Author: asushiye
 * @Date: 2020-07-02 15:30:23
 * @Commit: file content
 */
import { ChangeDetectorRef, Component, OnInit, ViewChild } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { Router } from '@angular/router';
import { AfMessageService, AfNavigationService } from '@core';
import { STColumn, STComponent } from '@delon/abc/st';
import { ModalHelper, _HttpClient } from '@delon/theme';
import { AfOperType } from 'src/app/core/af/af-const';
import { AfExcelService, ExcelUploadRecord } from 'src/app/routes/system/excel/af-excel.service';
import { ExcelUploadComponent } from 'src/app/routes/system/excel/upload/excel-upload.component';

import { CgDataBaseService } from './cg-database.service';

@Component({
  selector: 'app-cg-database',
  styleUrls: ['./cg-database.component.less'],
  templateUrl: './cg-database.component.html'
})
export class CgDataBaseComponent implements OnInit {
  rows: any[] = [];
  queryParams: any = { dbSourceName: '' };
  isLoading = false;
  isExport = false;
  isImport = false;

  afOperType = AfOperType.add;

  @ViewChild('st', { static: false }) st!: STComponent;
  columns: STColumn[] = [
    { title: '数据库', index: 'dataBase' },
    { title: '用途类型', index: 'ability' },
    { title: '数据库描述', index: 'remark' },
    { title: '支持版本', index: 'supportVersion' },
    { title: '创建日期', index: 'createdDate' },
    {
      title: '操作',
      buttons: [
        {
          text: '查看文档',
          icon: 'link',
          tooltip: `查看文档`,
          click: _record => {
            this.openDocUrl(_record);
          }
        },
        {
          text: '编辑',
          icon: 'edit',
          click: _record => {
            this.edit(_record);
          }
        },

        {
          text: '删除',
          icon: 'delete',
          type: 'del',
          tooltip: `删除此记录`,
          pop: {
            title: '您确定要删除此记录吗？',
            okType: 'danger',
            icon: 'star'
          },
          click: (record, _modal) => {
            this.delete(record);
          }
        }
      ]
    }
  ];

  constructor(
    private cdr: ChangeDetectorRef,
    public msg: AfMessageService,
    private dataBase: CgDataBaseService,
    private naviServ: AfNavigationService
  ) {}
  ngOnInit() {
    this.query();
  }

  public query() {
    this.isLoading = true;
    this.cdr.detectChanges();
    try {
      this.dataBase.queryAll().subscribe(body => {
        if (body.status) {
          this.rows = [];
          this.rows = body.data;
        } else {
          this.msg.notice_warning('查询文件', `警告编号${body.code};  警告信息${body.msg}`);
        }
      });
    } finally {
      setTimeout(() => {
        this.isLoading = false;
        this.cdr.detectChanges();
      }, 200);
    }
  }

  delete(item: any) {
    this.dataBase.delete(item.dataBase).subscribe(res => {
      if (res.status) {
        this.query();
        this.msg.msg_success('删除成功');
      } else {
        this.msg.notice_warning('警告', res.msg);
      }
    });
  }

  openDocUrl(item: any) {
    this.naviServ.openUrl(item.docUrl, true);
  }

  tForm = new FormGroup({
    dataBase: new FormControl('', { nonNullable: true, validators: [Validators.required] }),
    ability: new FormControl('', { nonNullable: true, validators: [Validators.required] }),
    remark: new FormControl('', { nonNullable: true, validators: [Validators.required] }),
    supportCase: new FormControl(''),
    supportVersion: new FormControl(''),
    jdbcFormat: new FormControl(''),
    driverClassName: new FormControl(''),
    scriptTable: new FormControl(''),
    scriptColumn: new FormControl(''),
    scriptConstraint: new FormControl(''),
    docUrl: new FormControl('')
  });

  drawerOption: any = { size: 'large', visible: false, title: '编辑' };
  openDrawer() {
    this.drawerOption.visible = true;
    if (this.afOperType == AfOperType.edit) {
      this.tForm.controls.dataBase.disable();
    } else {
      this.tForm.controls.dataBase.enable();
    }
  }

  closeDrawer() {
    this.drawerOption.visible = false;
  }

  save() {
    if (this.tForm.invalid) {
      this.msg.notice_warning('警告', '表单无效不能保存');
      return;
    }
    this.dataBase.save(this.tForm.getRawValue()).subscribe(res => {
      if (res.status) {
        this.query();
        this.msg.msg_success('保存成功');
        this.closeDrawer();
      } else {
        this.msg.notice_warning('警告', res.msg);
      }
    });
  }

  add() {
    this.afOperType = AfOperType.add;
    this.tForm.reset();
    this.tForm.controls.ability.patchValue('OLTP');
    this.tForm.controls.supportCase.patchValue('lower');
    this.openDrawer();
  }

  edit(item: any) {
    this.afOperType = AfOperType.edit;
    this.tForm.reset();
    this.tForm.patchValue(item);
    this.openDrawer();
  }
}
